/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.ila.basic.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.ila.basic.api.dto.MemberDTO;
import com.pig4cloud.ila.basic.api.entity.BasicMember;
import com.pig4cloud.ila.basic.service.BasicMemberService;
import com.pig4cloud.pig.common.core.constant.QueryConstants;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 会员表
 *
 * @author KonBAI
 * @date 2020-10-04 21:13:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/member" )
@Api(value = "member", tags = "会员表管理")
public class BasicMemberController {

    private final BasicMemberService memberService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param member 会员表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getCmsMemberPage(Page page, BasicMember member) {
        return R.ok(memberService.page(page, Wrappers.query(member)));
    }


    /**
     * 通过id查询会员表
     * @param member id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{member}")
    public R getById(@PathVariable("member" ) Integer member) {
        return R.ok(memberService.getById(member));
    }

    /**
     * 获取指定用户全部信息
     * @return 用户信息
     */
    @Inner
    @GetMapping("/info/{username}")
    public R info(@PathVariable String username) {
        BasicMember member = memberService.getOne(Wrappers.<BasicMember>query().lambda()
                .eq(BasicMember::getUsername, username)
                .last(QueryConstants.SELECT_LiMIT_ONE));
        if (member == null) {
            return R.failed(String.format("用户信息为空 %s", username));
        }
        return R.ok(memberService.getMemberInfo(member));
    }

    /**
     * 新增会员表
     * @param memberDTO 会员表
     * @return R
     */
    @ApiOperation(value = "新增会员表", notes = "新增会员表")
    @SysLog("新增会员表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_member_add')" )
    public R save(@RequestBody MemberDTO memberDTO) {
        return R.ok(memberService.saveMember(memberDTO));
    }

    /**
     * 修改会员表
     * @param member 会员表
     * @return R
     */
    @ApiOperation(value = "修改会员表", notes = "修改会员表")
    @SysLog("修改会员表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_member_edit')" )
    public R updateById(@RequestBody BasicMember member) {
        return R.ok(memberService.updateById(member));
    }

    /**
     * 通过id删除会员表
     * @param member id
     * @return R
     */
    @ApiOperation(value = "通过id删除会员表", notes = "通过id删除会员表")
    @SysLog("通过id删除会员表" )
    @DeleteMapping("/{member}")
    @PreAuthorize("@pms.hasPermission('basic_member_del')" )
    public R removeById(@PathVariable Integer member) {
        return R.ok(memberService.removeById(member));
    }

    /**
     * 注册会员
     * @param member 会员表
     * @return R
     */
    @Inner(value = false)
    @ApiOperation(value = "注册会员", notes = "注册会员")
    @SysLog("注册会员")
    @PostMapping("/register")
    public R register(@RequestBody BasicMember member) {
        return R.result(memberService.register(member));
    }

    /**
     * 获取当前会员信息
     * @return
     */
    @ApiOperation(value = "获取当前会员信息", notes = "获取当前会员信息")
    @SysLog("获取当前会员信息")
    @GetMapping("/current")
    public R currentMember() {
        Integer id = SecurityUtils.getMember().getId();
        BasicMember member = memberService.getById(id);
        if (ObjectUtil.isNull(member)) {
            return R.failed("找不到当前会员信息");
        }
        return R.ok(member);
    }

}
